package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url" 
    "io"   
    "os"
    "bytes"
    "encoding/json"
    "log"
    "time"
)
const HOST string = "http://127.0.0.1:8000"
type User struct {
  ID    uint   `json:"id"`
  Name  string `json:"name"`
  Email string `json:"email"`
  Createdat    time.Time `json:"createdat"`
  Updatedat    time.Time `json:"updatedat"`
}
func post1() {

    var ctime time.Time= time.Now()
    var user User
    user.Name="name"
    user.Email="ma@sina.com"
    user.Createdat=ctime
    user.Updatedat=ctime

    url := HOST+"/users"

    // data := []byte(`{"hello": "world"}`)
    data, err := json.Marshal(user)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
    if err != nil {
        log.Fatal("Error reading request. ", err)
    }

    // Set headers
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Host", "httpbin.org")

    // Create and Add cookie to request
    cookie := http.Cookie{Name: "cookie_name", Value: "cookie_value"}
    req.AddCookie(&cookie)

    // Set client timeout
    client := &http.Client{Timeout: time.Second * 10}

    // Validate cookie and headers are attached
    fmt.Println(req.Cookies())
    fmt.Println(req.Header)

    // Send request
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal("Error reading response. ", err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal("Error reading body. ", err)
    }

    fmt.Printf("%s\n", body)
}
func test_get() {
    resp, err := http.Get(HOST+"/rest/Todo")
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}
func test_post(){
    // resp, err := http.Post(HOST, "image/jpeg", &buf)
    // if err != nil {
    //     fmt.Println(err)
    // }
    // defer resp.Body.Close()
    // body, err := ioutil.ReadAll(resp.Body)
    // if err != nil {
    //     fmt.Println(err)
    // }
    // fmt.Println(string(body))
}
func test_postform(){
    resp, err := http.PostForm("http://www.baidu.com", url.Values{"wd": {"golang"}})
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(body))
}
func test_head() {
    resp, err := http.Head(HOST)
    if err != nil {
        fmt.Println("Request Failed: ", err.Error())
        return
    }
    defer resp.Body.Close() // 打印头信息
    for key, value := range resp.Header {
        fmt.Println(key, ":", value)
    }
}

func test_do() {
    // 初始化客户端请求对象
    req, err := http.NewRequest("GET", HOST, nil)
    if err != nil {
        fmt.Println(err)
        return
    }
    // 添加自定义请求头
    req.Header.Add("Custom-Header", "Custom-Value")
    // 其它请求头配置
    client := &http.Client{
        // 设置客户端属性
    }
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer resp.Body.Close()
    io.Copy(os.Stdout, resp.Body)
}
func main() {
    // test_get()
    // test_postform()
    // test_head()
    // test_do()
    post1()
}

